package thss.mro.simulate;

import java.util.Random;

import thss.mro.entity.Packet;
import thss.mro.entity.template.ProtocolTemplate;
import thss.mro.util.Constants;
import thss.mro.util.Protocol;
import thss.mro.util.QueryTester;
import thss.mro.util.Template;
/**
 * SCP模拟数据生成器
 */
public class SCPGenerator extends Generator {
	
	@Override
	public byte[] getData(){
		//初始化package
//		pack = new Packet();
		SCPCellGenerator cg = new SCPCellGenerator();
		//获得SCP头部模板以生成数据
		ProtocolTemplate scpHead = Protocol.getHeadTpl(Constants.PROTOCOL_NAME_SCP);
		byte[] headData = super.generateData(scpHead);
		//组织SCP正文数据（非根据正文模板自动化组织）
		Random r = new Random();
		int rNum = 0;
		while(rNum==0){//生成大于0的随机信元数
			rNum = r.nextInt(20);
		}
		int bodyLen = rNum*12+2;
		byte[] bodyData = new byte[bodyLen];
		int offset = 0;
		for(int i=0;i<rNum;i++){
			byte[] cellData = cg.getData();
			System.arraycopy(cellData, 0, bodyData, offset, cellData.length);
			offset += cellData.length;
		}
		byte[] checkCodeData = getRandomByteStream(2);
		System.arraycopy(checkCodeData, 0, bodyData, offset, checkCodeData.length);
		//组装头部和正文数据
		byte[] rst = new byte[headData.length+bodyData.length];
		System.arraycopy(headData, 0, rst, 0, headData.length);
		System.arraycopy(bodyData, 0, rst, headData.length, bodyData.length);
		return rst;
	}
	
//	@Override
//	protected byte[] getRandomByteStream(PacketField packFd,PacketTemplate pack){
//		if(packFd.getProtocolField().getName().equals("PacketLength")){ //包长度根据情况进行封装
//			byte[] value = new byte[packFd.getProtocolField().getLength()];
//			value[packFd.getProtocolField().getLength()-1]=(byte)(pack.getPacketLen() & 0xFF);;
//			return value;
//		}else{
//			return super.getRandomByteStream(packFd,pack);
//		}
//	}
	
	@SuppressWarnings("static-access")
	public static void main(String[] args){
//		Template t = new Template();
		QueryTester qt = new QueryTester();
		SCPGenerator sg = new SCPGenerator();
		for(int i=0;i<100;i++){
			byte[] data = sg.getData();
			System.out.println(data.length);
			qt.printHexString(data);
			System.out.println();
		}
	}
	
}
